﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Forms;

namespace e_TicaretAdmin_Application
{
    public partial class FrmKategori : Form
    {
        public FrmKategori()
        {
            InitializeComponent();
        }

        private void FrmKategori_Load(object sender, EventArgs e)
        {
            Tazele();
        }

        private void btnKategori_Click(object sender, EventArgs e)
        {
            var frm = new YeniKategori();
            frm.ShowDialog();
            Tazele();
        }

        private void Tazele(Expression<Func<Category, bool>> expression = null)
        {
            var categoriList = Program.Db.Categories.AsQueryable();
            if (expression != null)
                categoriList = categoriList.Where(expression);
            dataGridView1.DataSource =
                categoriList.Select(d => new { Kimlik = d.Id, KategoriAdi = d.Text, Seo = d.SeoText, Sırası = d.OrderNumber }).ToList();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var id = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            var frm = new YeniKategori { Id = id };
            frm.ShowDialog();
            Tazele();
        }

        private void txtKategori_TextChanged(object sender, EventArgs e)
        {
            Tazele(d => d.Text.Contains(txtKategori.Text) || d.SeoText.Contains(txtKategori.Text));
        }
    }
}
